package GuiLayer;
import ControlLayer.CtrOffer;
import ControlLayer.CtrOrder;
import ModelLayer.Offer;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import javax.swing.Action;

public class GUIOffer extends JFrame {

	private JPanel contentPane;
	private JTextField idInsert;
	private JTextField textCreateDate;
	private JTextField textPaymentDeadLine;
	private JTextArea offerText;
	private final Action action = new SwingAction();
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private final Action action_1 = new SwingAction_1();
	private JButton btnNewButton_1;
	private final Action action_2 = new SwingAction_2();
	private JButton btnNewButton_2;
	private final Action action_3 = new SwingAction_3();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GUIOffer frame = new GUIOffer();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public GUIOffer() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 533, 427);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblId = new JLabel("id");
		
		idInsert = new JTextField();
		idInsert.setColumns(10);
		
		JButton btnNewButton = new JButton("Opret Order");
		btnNewButton.setAction(action_1);
		
		textCreateDate = new JTextField();
		textCreateDate.setColumns(10);
		
		textPaymentDeadLine = new JTextField();
		textPaymentDeadLine.setColumns(10);
		
		offerText = new JTextArea();
		
		JButton btnGo = new JButton("Go");
		btnGo.setAction(action);
		
		lblNewLabel = new JLabel("Create Date");
		
		lblNewLabel_1 = new JLabel("Payment Deadline");
		
		btnNewButton_1 = new JButton("New button");
		btnNewButton_1.setAction(action_2);
		
		btnNewButton_2 = new JButton("New button");
		btnNewButton_2.setAction(action_3);
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
							.addGroup(gl_contentPane.createSequentialGroup()
								.addComponent(lblId)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(idInsert, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(btnGo)
								.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(btnNewButton))
							.addGroup(gl_contentPane.createSequentialGroup()
								.addGap(321)
								.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
									.addComponent(lblNewLabel)
									.addComponent(lblNewLabel_1))
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
									.addComponent(textCreateDate, Alignment.TRAILING)
									.addComponent(textPaymentDeadLine, Alignment.TRAILING))))
						.addComponent(offerText, GroupLayout.PREFERRED_SIZE, 297, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(62, Short.MAX_VALUE))
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
					.addContainerGap(289, Short.MAX_VALUE)
					.addComponent(btnNewButton_2)
					.addGap(12)
					.addComponent(btnNewButton_1)
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblId)
						.addComponent(idInsert, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnGo)
						.addComponent(btnNewButton))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(textCreateDate, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(textPaymentDeadLine, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_1))
					.addGap(6)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton_2)
						.addComponent(btnNewButton_1))
					.addPreferredGap(ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
					.addComponent(offerText, GroupLayout.PREFERRED_SIZE, 254, GroupLayout.PREFERRED_SIZE))
		);
		contentPane.setLayout(gl_contentPane);
	}
	
	private class SwingAction extends AbstractAction  
	{
		public SwingAction()
		{
			putValue(NAME, "Find Offer");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e)
		{
			try
			{
				findOfferId();
			}
			catch(Exception ex)
			{
				System.out.println("Offer could not be found");
			}
		}
	}
	
	private class SwingAction_1 extends AbstractAction 
	{
		public SwingAction_1() 
		{
			putValue(NAME, "Create Order");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) 
		{
			try
			{
				createOrder();
			}
			catch(Exception ex)
			{
				System.out.println("Order could not be Created");
			}
		}
	}
	
	private class SwingAction_2 extends AbstractAction 
	{
		public SwingAction_2() 
		{
			putValue(NAME, "EDIT");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) 
		{
			
		}
	}
	
	private class SwingAction_3 extends AbstractAction 
	{
		public SwingAction_3() 
		{
			putValue(NAME, "Delete");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) 
		{

            try {
                deleteOffer();
            } catch (Exception ex) {
                System.out.println("Offer not found");
            }
        }
	}
	
	
	public void findOfferId() throws Exception
	{
		offerText.setText("");
		String id = idInsert.getText();
		System.out.println(id);
		CtrOffer ctrlOff = new CtrOffer(); 
		Offer off = ctrlOff.findOfferId(id);


		double calculatedPrice = off.getCalculatedPrice();
        String dateStart = off.getDateStart();
        String dateEnd = off.getDateEnd();
        String acceptDeadline = off.getAcceptDeadline();
        double discount = off.getDiscount();
        String statusOffer = off.getStatusOffer();
        String sentDate = off.getSentDate();
        double pricePerHour = off.getPricePerHour();

        /**
         * If all the below is true the id is not in the table.
         */


        if (calculatedPrice == 0.0 && dateStart == null && dateEnd == null && acceptDeadline == null &&
                discount == 0.0 && statusOffer == null && sentDate == null && pricePerHour == 0.0)
        {
            offerText.setText("idOffer not found");
        }
        else
        {
            offerText.insert(
        		"CalculatedPrice = " + calculatedPrice + "\n" +
        		"Start Date = " + dateStart + "\n" +
        		"End Date = " + dateEnd + "\n" +
        		"Accepted DeadLine = " + acceptDeadline + "\n" +
        		"Discount = " + discount + "\n" +
        		"Offer Status = " + statusOffer + "\n" +
        		"Sent Date = " + sentDate + "\n" +
        		"Price per hour = " + pricePerHour, 0);
        }
	}


	public void createOrder() throws Exception
	{
		CtrOrder ctrOrd = new CtrOrder();
		CtrOffer ctrOff = new CtrOffer();

		String id = idInsert.getText();
		String createdDate = textCreateDate.getText();
		String paymentDeadline = textPaymentDeadLine.getText();
		String statusOrder = "Active";
		//offerStatus is changed to Accepted

        Offer off = ctrOff.findOfferId(id);

        if (createdDate.length() == 0 || paymentDeadline.length() == 0)
        {
            System.out.println("createdDate or paymentDeadline not filled in");
        }
        else{
		    ctrOrd.insertOrder(id, createdDate, paymentDeadline, statusOrder);
            ctrOff.updateOffer(id, off.getCalculatedPrice(), off.getDateStart(), off.getDateEnd(), off.getAcceptDeadline(), off.getDiscount(),"Pending", off.getSentDate(), off.getPricePerHour());
            System.out.println("Order created");
        }
	}

    public void deleteOffer() throws Exception {
        String id = idInsert.getText();

        CtrOffer ctrOff = new CtrOffer();
        ctrOff.deleteOffer(id);
        System.out.println("Order deleted");
    }
}

